import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule


class ByzwSpider(CrawlSpider):
    name = 'byzw'
    # allowed_domains = ['www.zwdu.com/book/30241']
    start_urls = ['http://www.zwdu.com/book/49382/22242816.html'] #    注意，起始页没有解析


    rules = (
        # Rule(LinkExtractor(allow=r'Items/'), callback='parse_item', follow=True),
        Rule(LinkExtractor(restrict_xpaths=r'//div[@class="bottem1"]/a[3]'), callback='parse_item', follow=True),
    )
    # Spider指定管道的代码
    custom_settings = {
        'ITEM_PIPELINES': {'scrapy_demo.pipelines.ByzwPipeline': 300},
    }

    def parse_item(self, response):
        item = {}
        #item['domain_id'] = response.xpath('//input[@id="sid"]/@value').get()
        #item['name'] = response.xpath('//div[@id="name"]').get()
        #item['description'] = response.xpath('//div[@id="description"]').get()
        title = response.xpath('//h1/text()').extract_first()
        print(response.url, 'title', title)
        yield {'title': title}
        # return item
